﻿using UnityEngine;
using System.Collections;

public class clusterbomb : MonoBehaviour {
	private GameObject clusters;
	GameObject ClusterRef;
	private float mytimer;
	public int numberOfClusters;
    public int liveTime;
    public float clusterBombSpeed = 100f;
	private bool clusterShot = false;
	// Use this for initialization
    GameObject[] clusterList;
    int i = 0;
	void Start () 
	{
		clusters = gameObject;
		mytimer = 0;
		var recognizer = new TKTapRecognizer();
					recognizer.gestureRecognizedEvent += ( r ) =>
			{
				clusterShot = true;
			};
			TouchKit.addGestureRecognizer( recognizer );
	}
	
	// Update is called once per frame
    void Update()
    {

        mytimer += Time.deltaTime;
        clusterList = GameObject.FindGameObjectsWithTag("cluster");


        if (clusterList.Length == 1 && clusterShot)
        {
            while( i < numberOfClusters)
            {
                float angle = i * Mathf.PI * 2 / numberOfClusters;
                Vector3 pos = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0) * 1;
                ClusterRef = Instantiate(clusters, transform.position + pos, Quaternion.Euler(new Vector3(Mathf.Rad2Deg * -angle, 90, 270))) as GameObject; //they all need to be facing in there own forward not a global forward
                ClusterRef.rigidbody.AddForce(ClusterRef.transform.forward * 50);
                i++;
            }
            mytimer = 0;
        }

          if (mytimer >= liveTime)
            foreach (GameObject c in clusterList)
            {
                Destroy(c);
            }
    } 
}
